package com.nine.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nine.entity.Announcement;
import com.nine.entity.Information;
import com.nine.entity.Message;
import com.nine.service.AnnouncementService;
import com.nine.service.InformationService;
import com.nine.service.MessageService;

@Controller
public class InformationController {
	@Autowired
	private InformationService informationService;
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private AnnouncementService announcementService;
	
	@org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) {
        // 不要删除下行注释!!! 将来"yyyy-MM-dd"将配置到properties文件中
        // SimpleDateFormat dateFormat = new
        // SimpleDateFormat(getText("date.format", request.getLocale()));
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd");  // HH:mm:ss
        dateFormat.setLenient(false);
        binder.registerCustomEditor(java.util.Date.class, null, new CustomDateEditor(
                dateFormat, true));
    }
	
	
	//添加资讯
	@RequestMapping("addinfo.do")
	public String addinformation( Information information , HttpSession session, @RequestParam(value="filebody") CommonsMultipartFile filebody,Model model) throws Exception{
		session.setAttribute("username", "admin");
		String username =(String) session.getAttribute("username");
		information.setPublisher(username);
		Calendar calendar = Calendar.getInstance();
		Date time = calendar.getTime();
		information.setSavetime(time);
		information.setSize((int)filebody.getSize());
		information.setFile(filebody.getBytes());
		
		int temp = informationService.insertSelective(information);
		if(temp>0) {
			model.addAttribute("notice", "成功");
			System.out.println("成功");
		}else {
			model.addAttribute("notice", "失败");
			System.out.println("失败");
		}
		return "/m_news/addInformation1.jsp";
	}
	//删除资讯
	@ResponseBody
	@RequestMapping("delInformation.do")
	public Map<String, Object> delInformation(Integer id)throws Exception{
	int temp=informationService.deleteByPrimaryKey(id);
	Map<String, Object> map=new HashMap<String, Object>();
	if(temp>0) {
		map.put("code",1);
		map.put("msg", "删除成功");
	}else {
		map.put("code",0);
		map.put("msg", "删除失败");
	}
	return map;
	}
	//删除多条公告
	@ResponseBody
	@RequestMapping("delInformation1.do")
	public Map<String, Object> delInformation(String ids)throws Exception{
	String[] id = ids.split(",");
	int temp=0;
	for (int i = 0; i < id.length; i++) {
		temp+=informationService.deleteByPrimaryKey(new Integer(id[i]));	
	}
	Map<String, Object> map=new HashMap<String, Object>();
	if(temp>0) {
		map.put("code",1);
		map.put("msg", "删除成功");
	}else {
		map.put("code",0);
		map.put("msg", "删除失败");
	}
	return map;
	}
	//修改资讯
	@RequestMapping("updateinformation.do")
	public String updateinformation(Information information ,HttpSession session, @RequestParam(value="filebody") CommonsMultipartFile filebody,Model model) throws Exception{
	int temp=informationService.updateByPrimaryKeySelective(information);
	if(temp>0) {
		model.addAttribute("notice", "成功");
		System.out.println("成功");
	}else {
		model.addAttribute("notice", "失败");
		System.out.println("失败");
	}
		return"/m_news/updateInformation.jsp";
	}
   //查询所有资讯
	@ResponseBody
	@RequestMapping("showInformation.do")
	public Map<String, Object> getAllInformation (Integer limit ,Integer page ) throws Exception{
		  //1.启用分页
		  PageHelper.startPage(page, limit); //传页码，页大小

	List<Information> list=	informationService.getAllInformation();
	
	  //3.获取分页后的相关信息
	  PageInfo<Information> pageInfo=new PageInfo<Information>(list);
	  Integer count = (int) pageInfo.getTotal();

	//layui对返回数据的json格式要求
	  Map<String,Object> resultMap = new HashMap<String, Object>();
	  resultMap.put("code", 0);
	  resultMap.put("msg", "");
	  resultMap.put("count", count);
	  resultMap.put("data", list);
	  return resultMap;

	}
//查询单条信息
	@RequestMapping("showInformationOne.do")
	public String getInformationOne(Integer id,Model model) throws Exception{
		Information information = informationService.selectByPrimaryKey(id);
		model.addAttribute("info", information);
		return "/m_news/updateInformation.jsp";
	
	} 

	
	
	//添加留言
	@RequestMapping("addmessage.do")
	public String addmessage(Message message , HttpSession session )throws Exception{
		session.setAttribute("username", "admin");
		String username =(String) session.getAttribute("username");
		message.setUserid(username);
		Calendar calendar = Calendar.getInstance();
		Date time = calendar.getTime();
		message.setMessagetime(time);
		message.setState(0);
		  int temp=messageService.insert(message);
		  
		  if(temp>0) { 
			  System.out.println("添加成功");
		  }else {
			  System.out.println("添加失败");
		  }
		return "m_news/news.jsp";
	}

	//删除留言
	@RequestMapping("delmessage.do")
	public String delmessage(String id) throws Exception{
	int temp=messageService.deleteByPrimaryKey(new Integer(id));
	 if(temp>0) {
		 System.out.println("删除成功");
	 }else {
		 System.out.println("删除失败");
	 }
		return "news.jsp";
	}
	//修改留言
	@RequestMapping("updatemessage.do")
	public String updatemessage(Message message)throws Exception{
	int temp=messageService.updateByPrimaryKey(message);
	if(temp>0) {
		System.out.println("修改成功");
	}else {
		System.out.println("修改失败");
	}
		return "news.jsp";
	}
	//查询所有留言
	@ResponseBody
	@RequestMapping("showMessage.do")
	public List<Message> getAllMessage() throws Exception{
	List<Message> listmessage=messageService.getAllMessage();
		return listmessage;
	}

	//添加公告
	@ResponseBody
	@RequestMapping("addAnnouncement.do")
    public Map<String, Object> addAnnouncement(@RequestBody Announcement announcement , HttpSession session) throws Exception{
		session.setAttribute("username", "admin");
		String username =(String) session.getAttribute("username");
		announcement.setPublisher(username);
		Calendar calendar = Calendar.getInstance();
		Date time = calendar.getTime();
		announcement.setPublishdate(time);

		int temp=announcementService.insert(announcement);
		Map<String, Object> map=new HashMap<String, Object>();
		if(temp>0) {
			map.put("code",1);
			map.put("msg", "添加成功");
		}else {
			map.put("code",0);
			map.put("msg", "添加失败");
		}
		return map;
	}
	//删除公告
		@ResponseBody
		@RequestMapping("delAnnouncement.do")
		public Map<String, Object> delAnnouncement(Integer id)throws Exception{
		int temp=announcementService.deleteByPrimaryKey(id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(temp>0) {
			map.put("code",1);
			map.put("msg", "删除成功");
		}else {
			map.put("code",0);
			map.put("msg", "删除失败");
		}
		return map;
		}
		
		//删除多条公告
		@ResponseBody
		@RequestMapping("delAnnouncement1.do")
		public Map<String, Object> delAnnouncement(String ids)throws Exception{
		String[] id = ids.split(",");
		int temp=0;
		for (int i = 0; i < id.length; i++) {
			temp+=announcementService.deleteByPrimaryKey(new Integer(id[i]));	
		}
		Map<String, Object> map=new HashMap<String, Object>();
		if(temp>0) {
			map.put("code",1);
			map.put("msg", "删除成功");
		}else {
			map.put("code",0);
			map.put("msg", "删除失败");
		}
		return map;
		}
	//修改公告
	@ResponseBody
	@RequestMapping("updateAnnouncement.do")
	public Map<String, Object> updateAnnouncement(@RequestBody Announcement announcement)throws Exception{
	
	int temp=announcementService.updateByPrimaryKey(announcement);
	Map<String, Object> map=new HashMap<String, Object>();
	if(temp>0) {
		map.put("code",1);
		map.put("msg", "修改成功");
	}else {
		map.put("code",0);
		map.put("msg", "修改失败");
	}
	return map;
	}
	
	//查询公告
	 @ResponseBody
	 @RequestMapping("showAnnouncement.do")
			public Map<String, Object> getAllAnnouncement (Integer limit ,Integer page ) throws Exception{
				  //1.启用分页
				  PageHelper.startPage(page, limit); //传页码，页大小

			List<Announcement> list=	announcementService.getAllAnnouncement();
			
			  //3.获取分页后的相关信息
			  PageInfo<Announcement> pageInfo=new PageInfo<Announcement>(list);
			  Integer count = (int) pageInfo.getTotal();

			//layui对返回数据的json格式要求
			  Map<String,Object> resultMap = new HashMap<String, Object>();
			  resultMap.put("code", 0);
			  resultMap.put("msg", "");
			  resultMap.put("count", count);
			  resultMap.put("data", list);
			  return resultMap;
	 }
	 // 查询单条
	 @ResponseBody
	 @RequestMapping("getAnnouncement.do")
	 public Map<String, Object> getAnnouncement(Integer id)throws Exception{
	Announcement announcement = announcementService.selectByPrimaryKey(id);
			Map<String, Object> map=new HashMap<String, Object>();
			if(announcement!=null) {
				map.put("code",1);
				map.put("msg", "修改成功");
				map.put("data", announcement);
			}else {
				map.put("code",0);
				map.put("msg", "修改失败");
				map.put("data", null);
			}
			return map;
	 }
}
